package selfview.sufutian.com.myapplication.draw2d;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import selfview.sufutian.com.myapplication.R;

/**
 * Created by sufutian on 2017/7/26.
 */

public class PathDemoView extends View {
    /**
     * 路径
     */
    private Path mPath;
    /**
     * 画笔
     */
    private Paint mPaint, mPaint2;
    /**
     * 路径测量类
     */
    private PathMeasure mPathMeasure, mPathMeasure2;

    private float mAnimatorValue;
    /**
     * 被截取的path
     */
    private Path mDst;
    /**
     * 获取的路径 长度
     */
    private float mLength;


    PathEffect mEffect;
    private Path path3;
    /**
     * 点的坐标
     */
    private float[] pos;
    /**
     * 切线的坐标
     */
    private float[] tan;


    private float[] arrowPos, arrowTan;
    private Bitmap arrowBitmap;
    private Matrix arrowMatrix;
    private PathMeasure arrowMeasure;

    public PathDemoView(Context context) {
        super(context);
        init(context);
    }

    public PathDemoView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mPathMeasure = new PathMeasure();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);

        mPaint2 = new Paint(mPaint);

        mPath = new Path();
        mPath.addCircle(400, 400, 200, Path.Direction.CW);//顺时针
        mPathMeasure.setPath(mPath, true);
        mLength = mPathMeasure.getLength();
        mDst = new Path();

        pos = new float[2];
        tan = new float[2];


        //路径绘制——DashPathEffect
        path3 = new Path();
        path3.moveTo(700, 200);
        path3.lineTo(1000, 200);
        path3.lineTo(700, 500);
        path3.close();
        mPathMeasure2 = new PathMeasure();
        mPathMeasure2.setPath(path3, true);
        final float length2 = mPathMeasure2.getLength();


        final ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mAnimatorValue = (float) valueAnimator.getAnimatedValue();//动画进度
                invalidate();
            }
        });
        valueAnimator.setDuration(2000);
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();


        //DashPathEffect
        final ValueAnimator valueAnimator2 = ValueAnimator.ofFloat(1, 0);
        valueAnimator2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float value = (float) valueAnimator.getAnimatedValue();//动画进度
                mEffect = new DashPathEffect(new float[]{length2, length2}, value * length2);
//                mEffect=new CornerPathEffect(90);
                mPaint2.setPathEffect(mEffect);//路径效果
                invalidate();
            }
        });
        valueAnimator2.setDuration(2000);
        valueAnimator2.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator2.start();


        arrowPos = new float[2];
        arrowTan = new float[2];
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;       // 缩放图片
        arrowBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.arrow, options);
        arrowMatrix = new Matrix();

        // 创建 PathMeasure
        arrowMeasure = new PathMeasure();

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mDst.reset();
        // 硬件加速的BUG
        //path片段绘制
        mDst.lineTo(0, 0);
        float stop = mLength * mAnimatorValue;
        mPathMeasure.getSegment(0, stop, mDst, true);
        canvas.drawPath(mDst, mPaint);
        //        float stop = mLength * mAnimatorValue;
//        float start = (float) (stop - ((0.5 - Math.abs(mAnimatorValue - 0.5)) * mLength));
//        mPathMeasure.getSegment(start, stop, mDst, true);
//        canvas.drawPath(mDst, mPaint);


        //获取正切值 和移动角度
        mPathMeasure.getPosTan(mPathMeasure.getLength() * mAnimatorValue, pos, tan);
        float degrees = (float) (Math.atan2(tan[1], tan[0]) * 180.0 / Math.PI);//Math.atan2()函数返回点(x,y)和原点(0,0)之间直线的倾斜角
        canvas.save();
        canvas.translate(400, 400);
        canvas.drawPath(mPath, mPaint);
        canvas.drawCircle(pos[0], pos[1], 10, mPaint);
        canvas.rotate(degrees);
        canvas.drawLine(0, -200, 300, -200, mPaint);
        canvas.restore();


        //-------------画出贝塞尔曲线 PATH----------------
        Path path = new Path();
        path.moveTo(100, 100);
        path.quadTo(400, 300, 500, 900);
        canvas.drawPath(path, mPaint);

        //直线path
        Path path2 = new Path();
        path2.moveTo(100, 100);
        path2.lineTo(500, 900);
        canvas.drawPath(path2, mPaint);

        //弧线path
        Path path1 = new Path();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            path1.addArc(200, 0, 600, 400, 0, 90);
            canvas.drawPath(path1, mPaint);
        }


        //DashPathEffect 冲刺绘制
        canvas.drawPath(path3, mPaint2);


        //bitmap箭头移动
        canvas.translate(400, 1000);
        Path arrowPath = new Path();                                 // 创建 Path
        arrowPath.addCircle(0, 0, 200, Path.Direction.CW);           // 添加一个圆形
        arrowMeasure.setPath(arrowPath, true);

        arrowMeasure.getPosTan(arrowMeasure.getLength() * mAnimatorValue, arrowPos, arrowTan);
        float arrowDegress = (float) (Math.atan2(arrowTan[1], arrowTan[0]) * 180.0 / Math.PI); // 计算图片旋转角度
        arrowMatrix.reset();
        arrowMatrix.postRotate(arrowDegress, arrowBitmap.getWidth() / 2, arrowBitmap.getHeight() / 2);   // 旋转图片
        arrowMatrix.postTranslate(arrowPos[0] - arrowBitmap.getWidth() / 2, arrowPos[1] - arrowBitmap.getHeight() / 2);// 将图片绘制中心调整到与当前点重合
        canvas.drawPath(arrowPath, mPaint);                                   // 绘制 Path
        canvas.drawBitmap(arrowBitmap, arrowMatrix, mPaint);


//        or 获取当前位置的坐标以及趋势的矩阵
//        arrowMeasure.getMatrix(arrowMeasure.getLength() * mAnimatorValue, arrowMatrix, PathMeasure.TANGENT_MATRIX_FLAG | PathMeasure.POSITION_MATRIX_FLAG);
//        arrowMatrix.preTranslate(-arrowBitmap.getWidth() / 2, -arrowBitmap.getHeight() / 2);   // <-- 将图片绘制中心调整到与当前点重合(注意:此处是前乘pre)
//        canvas.drawPath(arrowPath, mPaint);                                   // 绘制 Path
//        canvas.drawBitmap(arrowBitmap, arrowMatrix, mPaint);                     // 绘制箭头


    }
}
